import java.text.ParseException;
import java.text.SimpleDateFormat;
import org.joda.time.DateTime;

public class AddParser {

	public Task parse(String data) throws ParseException {


		/*
		 * private static int count = 1; private int id; private String content;
		 * private Calendar deadline; private Calendar start; private Calendar
		 * end; private boolean priority; private String label; private boolean
		 * done; private String userInput;
		 */
		// [add][space][task_content][space][-by][space][date]
		// [task_content][bydate]
		// [task_content][fromdate][todate]
		// [add][space][task_content][space][-from][space][date][space][-to][date]
		Task newTask = null;
		String taskContent = "";
		DateTime deadline;
		DateTime startDate;
		DateTime endDate;
		String[] dataComponents;

		SimpleDateFormat ddmmyyyy = new SimpleDateFormat("dd/MM/yyyy");
		data = data.trim();

		// check if it is a basic task or a task with keywords

		if (!data.contains("-")) {
			taskContent = data;
			newTask = new Task(data);
		} else {
			if (data.contains("-by")) {
				dataComponents = data.split("-by");
				taskContent = dataComponents[0].trim();
				deadline = new DateTime(ddmmyyyy.parse(dataComponents[1]));
				newTask = new Task(taskContent, deadline);
			} else {
				dataComponents = data.split("-from");
				taskContent = dataComponents[0];
				startDate = new DateTime(ddmmyyyy.parse(dataComponents[1]));
				dataComponents = data.split("-to");
				endDate = new DateTime(ddmmyyyy.parse(dataComponents[1]));
				newTask = new Task(taskContent, startDate, endDate);
			}
			if (data.contains("-label")) {
				dataComponents = data.split("-label");
				if (dataComponents[1].contains("!")) {
					dataComponents = dataComponents[1].split("!");
					newTask.setLabel(dataComponents[0].trim());
					newTask.setPriority(true);
				} else {
					newTask.setLabel(dataComponents[1].trim());
				}

			}
			if (data.contains("!")) {
				newTask.setPriority(true);
			}

		}

		// DateTime dt = new DateTime();
		// int month = dt.getMonthOfYear();
		newTask.setUserInput(data);
		return newTask;
	}

}
